package heima3_2.cn.osc.ui.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import net.oschina.app.R;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.OnekeyShare;
import heima3_2.cn.osc.base.BaseFragment;
import heima3_2.cn.osc.constant.Constants;
import heima3_2.cn.osc.model.bean.BlogBean;
import heima3_2.cn.osc.model.bean.GeneralNewsBean;
import heima3_2.cn.osc.ui.main_fragment.synthesize_particulars_fragment.BloggerParticularsFragment;
import heima3_2.cn.osc.ui.main_fragment.synthesize_particulars_fragment.ConsultParticularsFragment;
import heima3_2.cn.osc.ui.main_fragment.synthesize_particulars_fragment.HopstotParticularsFragment;
import heima3_2.cn.osc.ui.main_fragment.synthesize_particulars_fragment.RecommendParticularsFragment;
import heima3_2.cn.osc.utils.PreferencesUtils;

/**
 * Created by Administrator on 2017/1/16 0016.
 */

public class ComprehensiveActivity extends AppCompatActivity {
    @Bind(R.id.btn_back)
    ImageButton mBtnBack;
    @Bind(R.id.tv_particulars_title)
    TextView mparticularsTvTitle;
    @Bind(R.id.iv_refresh)
    ImageView mIvRefresh;
    @Bind(R.id.tv_title)
    TextView mTvTitle;
    @Bind(R.id.tv_pubdate)
    TextView mTvPubdate;
    @Bind(R.id.tv_commentcount)
    TextView mTvCommentcount;
    @Bind(R.id.iv_keyboard_input)
    ImageView mIvKeyboardInput;
    @Bind(R.id.iv_comment)
    ImageView mComment;
    @Bind(R.id.iv_input)
    ImageView mIvInput;
    @Bind(R.id.iv_like)
    ImageView mIvLike;
    @Bind(R.id.iv_share)
    ImageView mIvShare;
    @Bind(R.id.ll_comptehen_top)
    LinearLayout mLlComptehenTop;
    @Bind(R.id.iv_write)
    ImageView mIvWrite;
    @Bind(R.id.et_inpute)
    EditText mEtInpute;
    @Bind(R.id.iv_expression)
    ImageView mIvExpression;
    @Bind(R.id.iv_return)
    ImageView mIvReturn;
    @Bind(R.id.ll_comptehen_but)
    LinearLayout mLlComptehenBut;
    @Bind(R.id.fl_comptehen)
    FrameLayout mFlComptehen;
    @Bind(R.id.iv_warn)
    ImageView mIvWarn;
    private GeneralNewsBean.NewsBean mNewsBean;
    private int mAdvisory_details;
    int CONSULT_PARTICULARS = 1;
    int HOTSPOT_PARTICULARS = 2;
    int BLOGS_PARTICULARS = 3;
    int RECOMMEND_PARTICULARS = 4;
    private GeneralNewsBean.NewsBean mHostBean;
    private int host_details;
    private BlogBean mBlogBean;
    private int blog_details;
    private int mBlog_details;
    private BlogBean mRecommendBean;
    private int mRecommed_details;
    boolean isTop = true;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_comprehensive);
        ButterKnife.bind(this);
        iniData();
        iniPageData();
        ShareSDK.initSDK(this, "1ae3f7e9cdb38");

    }

    private void iniPageData() {
        BaseFragment fragment = null;
        if (mAdvisory_details == CONSULT_PARTICULARS) {
            mTvTitle.setText("咨询详情");
            mparticularsTvTitle.setText(mNewsBean.getTitle());
            mTvPubdate.setText(mNewsBean.getPubDate());
            mTvCommentcount.setText(mNewsBean.getCommentCount() + "");
            fragment = new ConsultParticularsFragment();
            int id = mNewsBean.getId();
            System.out.println("id" + id);
            PreferencesUtils.setInt(getApplicationContext(), Constants.NEWS_ID, mNewsBean.getId());
        } else if (host_details == HOTSPOT_PARTICULARS) {
            mTvTitle.setText("热点详情");
            mparticularsTvTitle.setText(mHostBean.getTitle());
            mTvPubdate.setText(mHostBean.getPubDate());
            mTvCommentcount.setText(mHostBean.getCommentCount() + "");
            fragment = new HopstotParticularsFragment();
            PreferencesUtils.setInt(getApplicationContext(), Constants.HOTS_ID, mHostBean.getId());
        } else if (mBlog_details == BLOGS_PARTICULARS) {
            mTvTitle.setText("博客详情");
            mparticularsTvTitle.setText(mBlogBean.title);
            mTvPubdate.setText(mBlogBean.pubDate);
            mTvCommentcount.setText(mBlogBean.commentCount + "");
            fragment = new BloggerParticularsFragment();
            PreferencesUtils.setInt(getApplicationContext(), Constants.BOGGER_ID, mBlogBean.id);
            System.out.println("mBlogBean.id" + mBlogBean.id);
        } else if (mRecommed_details == RECOMMEND_PARTICULARS) {
            mTvTitle.setText("推荐详情");
            mparticularsTvTitle.setText(mRecommendBean.title);
            mTvPubdate.setText(mRecommendBean.pubDate);
            mTvCommentcount.setText(mRecommendBean.commentCount + "");
            fragment = new RecommendParticularsFragment();
            PreferencesUtils.setInt(getApplicationContext(), Constants.RECOMMEND_ID, mRecommendBean.id);
        }
        FragmentTransaction trans = getSupportFragmentManager()
                .beginTransaction();
        trans.replace(R.id.fl_fragment, fragment);
        trans.commitAllowingStateLoss();
    }

    private void iniData() {

        iniConsultData();
        iniHotsData();
        iniBoggerData();
        iniRecommendData();
    }

    private void iniRecommendData() {
        Bundle bundle = getIntent().getExtras();
        mRecommendBean = (BlogBean) bundle.get("RECOMMEND");
        mRecommed_details = getIntent().getIntExtra("ADVISORY_RECOMMEND", 0);
    }

    private void iniBoggerData() {
        Bundle bundle = getIntent().getExtras();
        mBlogBean = (BlogBean) bundle.get("BLOG");
        mBlog_details = getIntent().getIntExtra("ADVISORY_BLOG", 0);

    }

    private void iniHotsData() {
        Bundle bundle = getIntent().getExtras();
        mHostBean = (GeneralNewsBean.NewsBean) bundle.get("HOST");
        host_details = getIntent().getIntExtra("ADVISORY_HOST", 0);
    }

    private void iniConsultData() {
        Bundle bundle = getIntent().getExtras();
        mNewsBean = (GeneralNewsBean.NewsBean) bundle.get("NEWS");
        mAdvisory_details = getIntent().getIntExtra("ADVISORY_DETAILS", 0);
    }

    @OnClick({R.id.btn_back, R.id.iv_return, R.id.iv_keyboard_input, R.id.iv_comment, R.id.iv_input
            , R.id.iv_like, R.id.iv_share, R.id.iv_write,R.id.iv_warn})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_back:
                finish();
                break;
            case R.id.iv_return:
                break;
            case R.id.iv_keyboard_input:
                //键盘输入
                beyboard();
                break;
            case R.id.iv_comment:
                //评论
                break;
            case R.id.iv_input:
                //手写

                break;
            case R.id.iv_like:
                //登录

                break;
            case R.id.iv_share:
                //分享
                showShare();
                break;
            case R.id.iv_write:
                inputAnimain();
                break;
            case R.id.iv_warn:
                //举报
                    btnWarn();
                break;
        }
    }

    private void btnWarn() {

    }

    private void inputAnimain() {
        // mLlComptehenBut.setVisibility(View.VISIBLE);
        TranslateAnimation ta = new TranslateAnimation(
                TranslateAnimation.RELATIVE_TO_SELF, 0,
                TranslateAnimation.RELATIVE_TO_SELF, 0,
                TranslateAnimation.RELATIVE_TO_SELF, 0,
                TranslateAnimation.RELATIVE_TO_SELF, 1);
        //ta.setRepeatMode(Animation.RESTART);
        ta.setDuration(200);
        //ta.setFillAfter(true);
        mLlComptehenBut.startAnimation(ta);
        ta.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                mLlComptehenTop.setVisibility(View.VISIBLE);
                mLlComptehenBut.setVisibility(View.GONE);
                mIvKeyboardInput.setEnabled(true);
                mIvWrite.setEnabled(false);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
    }

    private void animationWrite() {
    }

    private void beyboard() {
        mLlComptehenBut.setVisibility(View.GONE);
        mLlComptehenTop.setVisibility(View.VISIBLE);
        mIvWrite.setEnabled(false);

        TranslateAnimation ta = new TranslateAnimation(
                TranslateAnimation.RELATIVE_TO_SELF, 0,
                TranslateAnimation.RELATIVE_TO_SELF, 0,
                TranslateAnimation.RELATIVE_TO_SELF, 0,
                TranslateAnimation.RELATIVE_TO_SELF, 1);
        ta.setRepeatMode(Animation.RESTART);
        ta.setDuration(200);
        // ta.setFillAfter(true);
        mLlComptehenTop.startAnimation(ta);
        ta.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                mLlComptehenBut.setVisibility(View.VISIBLE);
                mIvKeyboardInput.setEnabled(false);
                mIvWrite.setEnabled(true);
                mLlComptehenTop.setVisibility(View.GONE);

            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });


    }

    public void beyTopAniman(int height) {

        //  ta.setFillEnabled(true);
        if (isTop) {
            isTop = false;
            mLlComptehenTop.animate().translationY(height);
            mLlComptehenBut.setVisibility(View.GONE);
        } else {
            isTop = true;
            mLlComptehenBut.setVisibility(View.VISIBLE);
            TranslateAnimation ta = new TranslateAnimation(
                    TranslateAnimation.RELATIVE_TO_SELF, 0,
                    TranslateAnimation.RELATIVE_TO_SELF, 0,
                    TranslateAnimation.RELATIVE_TO_SELF, 0,
                    TranslateAnimation.RELATIVE_TO_SELF, 1);
            ta.setDuration(1000);
            mLlComptehenBut.startAnimation(ta);
        }
        //beyButAniman(height);
        // mLlComptehenTop.animate().translationY(-height);
        //mLlComptehenTop.animate().t
    }

    private void beyButAniman(int height) {

        mLlComptehenBut.setVisibility(View.VISIBLE);
      /*  TranslateAnimation ta = new TranslateAnimation(
                TranslateAnimation.RELATIVE_TO_SELF, 0,
                TranslateAnimation.RELATIVE_TO_SELF, 0,
                TranslateAnimation.RELATIVE_TO_SELF, 0,
                TranslateAnimation.RELATIVE_TO_SELF, 1);
                      ta.setDuration(1000);
        //  ta.setFillEnabled(true);
        mLlComptehenBut.startAnimation(ta);
        ta.setRepeatCount(1);
        ta.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                isTop=false;
                mLlComptehenBut.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAnimationEnd(Animation animation) {
               // mLlComptehenTop.setVisibility(View.GONE);
                //beyButAniman();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });*/

    }

    private void showShare() {

        ShareSDK.initSDK(this);

        OnekeyShare oks = new OnekeyShare();

//关闭sso授权

        oks.disableSSOWhenAuthorize();


// title标题，印象笔记、邮箱、信息、微信、人人网和QQ空间等使用

        oks.setTitle("标题");

// titleUrl是标题的网络链接，QQ和QQ空间等使用

        oks.setTitleUrl("http://sharesdk.cn");

// text是分享文本，所有平台都需要这个字段

        oks.setText("我是分享文本");

// imagePath是图片的本地路径，Linked-In以外的平台都支持此参数

//oks.setImagePath("/sdcard/test.jpg");//确保SDcard下面存在此张图片

// url仅在微信（包括好友和朋友圈）中使用

        oks.setUrl("http://sharesdk.cn");

// comment是我对这条分享的评论，仅在人人网和QQ空间使用

        oks.setComment("我是测试评论文本");

// site是分享此内容的网站名称，仅在QQ空间使用

        oks.setSite(getString(R.string.app_name));

// siteUrl是分享此内容的网站地址，仅在QQ空间使用

        oks.setSiteUrl("http://sharesdk.cn");


// 启动分享GUI

        oks.show(this);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
    }

}
